package com.simplehire.service;

import com.simplehire.model.User;
import com.simplehire.model.UserRole;
import com.simplehire.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class DataInitializer implements CommandLineRunner {
    
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;
    
    public DataInitializer(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        this.userRepository = userRepository;
        this.passwordEncoder = passwordEncoder;
    }
    
    @Override
    public void run(String... args) throws Exception {
        // 检查是否已存在管理员账户
        if (userRepository.findByUsername("admin").isEmpty()) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setEmail("admin@simplehire.com");
            admin.setFullName("系统管理员");
            admin.setRoles(Arrays.asList(UserRole.ADMIN));
            admin.setIsActive(true);
            
            userRepository.save(admin);
            System.out.println("已创建默认管理员账户: admin/admin123");
        }
    }
}